package csel.view;

import java.awt.Color;

import csel.model.Entity;
import csel.model.Observer;

import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.ImageIcon;


/**
 * Reasons for Existence: Encapsulates the part of the status viewport that displays the player's avatar. 
 * This is a distinct function and should be differentiated from the other features of the StatusViewport.
 * 
 * @author Alex
 */
public class AvatarViewer extends JPanel implements Observer
{
	private static final long serialVersionUID = 1L;
	
	private ImageIcon playerPic;
	private JLabel avatarName;
	private JLabel avatarPic;
	private JLabel avatarOcc;
	
	/**
	 * current entity being watched
	 */
	private Entity cEntity;
	
	public AvatarViewer(Entity e)
	{
		e.addObserver(this);
		
		setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
        playerPic = new ImageIcon(RepresentationFactory.getInstance().get(e).getSimpleImage());
        avatarName = new JLabel(e.getName(),JLabel.CENTER);
        avatarPic = new JLabel(playerPic,JLabel.CENTER);
        avatarOcc = new JLabel(e.getOccupation().toString(),JLabel.CENTER);
        
        avatarName.setForeground(Color.WHITE);
        avatarOcc.setForeground(Color.WHITE);
        
        add(avatarPic);
        add(avatarName);
        add(avatarOcc);
	}
	
	public void setEntity(Entity e)
	{
		if (cEntity != null)
			cEntity.removeObserver(this);
		
		System.out.printf("[VIEW] AvatarViewer setting new Entity (%s).\n", e);
		
		cEntity = e;
		cEntity.addObserver(this);
		modelUpdate(cEntity);
	}
	
	public void modelUpdate(Object o)
	{
		if (o == cEntity)
		{
			playerPic.setImage(RepresentationFactory.getInstance().get(cEntity).getSimpleImage());
			avatarName.setText(cEntity.getName());
	        avatarPic.setIcon(playerPic);
	        avatarOcc.setText(cEntity.getOccupation().toString());
	        
	        revalidate();
		}
		else
		{
			System.out.printf("[VIEW] Warning: AvatarViewer got a bad object (%s).\n", o);
		}
	}
	
	private void writeObject(java.io.ObjectOutputStream out) throws java.io.IOException
	{
		throw new java.io.NotSerializableException("AvatarViewer should not be serialized.");
	}
	
	private void readObject(java.io.ObjectInputStream in) throws java.io.IOException
	{
		throw new java.io.NotSerializableException("AvatarViewer should not be serialized.");
	}
}